<?php

namespace App\Http\Controllers\Api;

use App\Answers;
use App\Classify;
use Illuminate\Http\Request;
use App\Topic;
use App\Answer;
use App\TopicAnswer;
use App\TopicAnswerCount;
use App\TopicAnswerWrite;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class TopicController extends Controller
{
    public function getTopicList(){
        $classifys = Classify::orderBy("sort","asc")->orderBy("id","asc")->get();
        
        if(count($classifys) > 0){
            foreach($classifys as $classify){
                $allTopic = Topic::whereClassifyId($classify->id)->orderBy('created_at','asc')->get();
                if(count($allTopic) > 0){
                    foreach ($allTopic as $topic){
                        $answers = Answer::whereTaId($topic->id)->orderBy('answer_num','asc')->get();
                        $topic->answers = $answers;
                    }
                }

                $classify->questions = $allTopic;
            }
        }

        return response()->json(["level"=>"success","message"=>"获取成功","data"=>$classifys]);
    }

    public function setTopicScore(Request $request){
        $name       = $request->get('name');
        $phone      = $request->get('phone');
        $unitName   = $request->get('unit_name');
        $qInfo      = $request->get('q_info');

        if($name == null)
            return response()->json(['level' => 'error','message' => '请填写姓名','data' => 'not_fill_name']);

        if($unitName == null)
            return response()->json(['level' => 'error','message' => '请填写单位名称','data' => 'not_fill_unit_name']);

//        $allQuestionCount = Topic::all();
//        if(count($qInfo) != count($allQuestionCount)){
//            $num = count($allQuestionCount) - count($qInfo);
//
//            return response()->json(['level' => 'error','message' => 'not_all','data' => $num]);
//        }

        $newTopicAnswer = new TopicAnswer();
        $newTopicAnswer->name       = $name;
        $newTopicAnswer->phone      = $phone;
        $newTopicAnswer->unit_name  = $unitName;
        $newTopicAnswer->save();

        $fen = 0;
        if(is_array($qInfo)){
            foreach($qInfo as $theInfo){
                $thatTopic = Topic::whereId($theInfo['qid'])->first();
                $aids = $theInfo["aids"];
                if(!is_array($aids)){
                    return response()->json(['level' => 'error','message' => '答案数据格式错误']);
                }

                if(count($aids) == 0){
                    return response()->json(['level' => 'error','message' => '您有题目未选择答案，请检查！']);
                }

                if($thatTopic->type==1){
                    if($aids[0] == ""){
                        return response()->json(['level' => 'error','message' => '您有题目未选择答案，请检查！']);
                    }
                    $thatAnswer = Answer::whereId($aids[0])->first();
                    if($thatAnswer){
                        if($thatAnswer->select_answer == 1){
                            $theFen = $thatTopic->fraction;
                            $fen = $fen + $theFen;
                        }else{
                            $theFen = 0;
                        }
                        $newTopicAnswerWrite = new TopicAnswerWrite();
                        $newTopicAnswerWrite->t_id = $thatTopic->id;
                        $newTopicAnswerWrite->a_id = $thatAnswer->id;
                        $newTopicAnswerWrite->tac_id = $newTopicAnswer->id;
                        $newTopicAnswerWrite->fen    = $theFen;
                        $newTopicAnswerWrite->save();
                    }
                }else{
                    $aidStr  = "";
                    foreach ($aids as $aid){
                        if($aidStr == ""){
                            $aidStr = $aid;
                        }else{
                            $aidStr = $aidStr . "," . $aid;
                        }
                    }

                    if($aidStr == ""){
                        return response()->json(['level' => 'error','message' => '您有题目未选择答案，请检查！']);
                    }

                    $allRightAnswer = Answer::whereTaId($thatTopic->id)->whereSelectAnswer(1)->pluck("id")->toArray();

                    $newTopicAnswerWrite = new TopicAnswerWrite();
                    $diff1 = array_diff($aids,$allRightAnswer);
                    $diff2 = array_diff($allRightAnswer,$aids);
                    if(count($diff1) == 0 && count($diff2) == 0){
                        $newTopicAnswerWrite->fen = $thatTopic->fraction;
                        $fen = $fen + $thatTopic->fraction;
                    }
                    $newTopicAnswerWrite->t_id   = $thatTopic->id;
                    $newTopicAnswerWrite->a_id   = $aidStr;
                    $newTopicAnswerWrite->tac_id = $newTopicAnswer->id;
                    $newTopicAnswerWrite->save();
                }

            }
        }else{
            return response()->json(['level' => 'error','message' => '数据格式错误']);
        }

        $newTopicAnswer->score = $fen;
        $newTopicAnswer->save();

        return response()->json(["level"=>"success","message"=>"提交成功","data"=> $newTopicAnswer]);
    }
}
